//
//  YAYIGetSystemPicView.m
//  多点执业
//
//  Created by hxp on 16/6/23.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIGetSystemPicView.h"

@implementation YAYIGetSystemPicView
- (instancetype)initWithFrame:(CGRect)frame viewController:(UIViewController *)controller
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createActionView:controller];
        self.controller = controller;
    }
    return self;
}


-(void)createActionView:(UIViewController *)controller{
    
    self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"打开相册" otherButtonTitles:@"拍照", nil];
    [self.actionSheet  showInView:controller.view];
}


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [self LocalPhoto];
            break;
        case 1:
            [self takePhoto];
            break;
        default:
            break;
    }
}
-(void)LocalPhoto{
    YAYIPhotoGroupsController *group = [YAYIPhotoGroupsController new];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:group];
    group.delagate = self;
    //[self.navigationController pushViewController:navigation animated:YES];
    [self.controller presentViewController:navigation animated:YES completion:nil];
}
//拍照
-(void)takePhoto{
    //资源类型为照相机
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    //判断是否有相机
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        //资源类型为照相机
        picker.sourceType = sourceType;
        [self.controller presentModalViewController:picker animated:YES];
        
    }else {
        YAYILog(@"该设备无摄像头");
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    //UIImagePickerControllerOriginalImage
    //当图片不为空时显示图片并保存图片
    if (image != nil) {
        //图片显示在界面上
        //[changeImg setBackgroundImage:image forState:UIControlStateNormal];
        if (_openSystemPhotoOperation != nil) {
            self.openSystemPhotoOperation(image);
        }
        //以下是保存文件到沙盒路径下
        //把图片转成NSData类型的数据来保存文件
        NSData *data;
        //判断图片是不是png格式的文件
        if (UIImagePNGRepresentation(image)) {
            //返回为png图像。
            data = UIImagePNGRepresentation(image);
        }else {
            //返回为JPEG图像。
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        //保存

    }
    //关闭相册界面
    [picker dismissModalViewControllerAnimated:YES];
}
-(void)imageAry:(NSArray *)images{
    for (UIImage *image in images) {
        self.openSystemPhotoOperation(image);
    }

}
-(void)openSystemPhotoBlock:(OpenSystemPhotoOperation)block
{
    self.openSystemPhotoOperation = block;
}
@end
